package com.xiaohu.myspace.action;

import com.xiaohu.myspace.buiness.buinessinterface.UserInfoInterface;
import com.xiaohu.myspace.database.entity.UserInfo;
import com.xiaohu.myspace.utils.BASE64;

public class UserInfoAction extends BaseAction {
	private static final long serialVersionUID = 8169132436889126813L;

	private UserInfoInterface userInfoInterface;
	private UserInfo user;

	private String returnData;

	public String getReturnData() {
		return returnData;
	}

	public void setReturnData(String returnData) {
		this.returnData = returnData;
	}

	public UserInfoInterface getUserInfoInterface() {
		return userInfoInterface;
	}

	public void setUserInfoInterface(UserInfoInterface userInfoInterface) {
		this.userInfoInterface = userInfoInterface;
	}

	public UserInfo getUser() {
		return user;
	}

	public void setUser(UserInfo user) {
		this.user = user;
	}

	public String regiest() {
		if (null == user) {
			return ERROR;
		}
		try {
			user.setUserId(BASE64.encryptBASE64((user.getUserName() + user
					.getUserPassword()).getBytes()));
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		boolean isTure = userInfoInterface.insertUserInfo(user);
		System.out.println("insertUser=" + isTure);
		if (isTure) {
			if (user.getClientType() == 1) {
				return returnPage;
			} else if (user.getClientType() == 2) {
				return returnData;
			}
			return returnPage;
		} else {
			if (user.getClientType() == 1) {
				return ERROR;
			} else if (user.getClientType() == 2) {
				return returnData;
			}
			return ERROR;
		}
	}

	public String login() {
		if (null == user) {
			return ERROR;
		}
		UserInfo userInfo = userInfoInterface.loginWithUserInfo(user);
		if (null != userInfo) {
			returnData = initReturnData(SUCCESSCODE, user);
			System.out.println(returnData);
			if (user.getClientType() == 1) {
				return returnPage;
			} else if (user.getClientType() == 2) {
				return returnData;
			}
			return returnPage;
		} else {
			if (user.getClientType() == 1) {
				return ERROR;
			} else if (user.getClientType() == 2) {
				return returnData;
			}
			return ERROR;
		}
	}

}
